import RequestProcess from "./RequestProcess";
import operator from "../operator/Operator";
import OperateArgs from "../operator/OperateArgs";
import CheckUserExistOperator from "../operator/CheckUserExistOperator";
import OperatorDecorator from "../operator/OperatorDecorator";
import GenerateIdOperator from "../operator/GenerateIdOperator";
import JwtGenerateOperator from "../operator/jwtGenerateOperator";
import EncryptPasswordOperator from "../operator/EncryptPasswordOperator";
import CreateUserObjectOperator from "../operator/CreateUserObjectOperator";
import {RC} from "../config/RC";
import CreateUserMessageBoxOperator from "../operator/CreateUserMessageBoxOperator";

class RegisterRequestProcess extends RequestProcess {

    public args_generate(): void {
        this.args = new OperateArgs();
        this.args.setProperty("userName", this.req.body.userName);
        this.args.setProperty("password", this.req.body.password);
    }

    public operator_plan_generate(): void {
        const check_user_exist_operator = new CheckUserExistOperator(this.args);
        const generate_user_operator = new GenerateIdOperator(this.args);
        const encrypt_password_operator = new EncryptPasswordOperator(this.args);
        const create_user_object_operator = new CreateUserObjectOperator(this.args);
        const create_messageBox_object_operator = new CreateUserMessageBoxOperator(this.args);
        const jwt_generate_operator = new JwtGenerateOperator(this.args);

        this.operator_plan = new OperatorDecorator(generate_user_operator, check_user_exist_operator);
        this.operator_plan = new OperatorDecorator(encrypt_password_operator, this.operator_plan);
        this.operator_plan = new OperatorDecorator(create_user_object_operator, this.operator_plan);
        this.operator_plan = new OperatorDecorator(create_messageBox_object_operator, this.operator_plan);
        this.operator_plan = new OperatorDecorator(jwt_generate_operator, this.operator_plan);

    }

    public writeResult(rc: RC): Object {
        switch (rc) {
            case RC.SUCCESS:
                return {
                    code: "0000",
                    msg: "注册成功",
                    userId: this.getOperateArgs().getProperty("generateId"),
                    token: this.getOperateArgs().getProperty("token")
                }
            case RC.USERNAME_EXIST:
                return {
                    code: "1001",
                    msg: "用户名已存在"
                }
            case RC.INVALID_ARGUMENT:
                return {
                    code: "1002",
                    msg: "参数不合法"
                }
            case RC.INTERNAL_ERROR:
                return {
                    code: "1003",
                    msg: "服务器内部错误"
                }
            default:
                return {
                    code: "2000",
                    msg: "未知错误"
                }
        }
    }
}

export default RegisterRequestProcess;